﻿package com.devilcat.ui {	
	import flash.display.MovieClip;	
	import flash.events.MouseEvent;
	import com.devilcat.dt.Pokemondt;	
	import com.devilcat.dt.Gamedt;
	import com.devilcat.dt.Itemdt;
	import com.devilcat.ob.Itemob;
	import flash.events.Event;
	import flash.display.Loader;
	import flash.net.URLRequest;

	public class PokemonInfor extends MovieClip {
		
		public var pokemon:Pokemondt;
		public var replacePokemon:Pokemondt;
		public var itemOut:Itemob;
		public function PokemonInfor(_pokemon:Pokemondt) {
			pokemon=_pokemon;
			AddEventShowInfor();
		}
		//----------------------------------------
		public function AddEventShowInfor(){
			ReloadText();
			LoadMovesPanel();	
				var imageLoader:Loader = new Loader();
				var imageLink:URLRequest = new URLRequest("front/"+Gamedt.StringID(pokemon.pid)+".png");
				imageLoader.load(imageLink);
				imageLoader.contentLoaderInfo.addEventListener(Event.INIT,imageLoaded);
				function imageLoaded(e:Event):void {
					LoadPokedexIMG.addChild(imageLoader);
				}
			this.addEventListener(MouseEvent.MOUSE_OVER,ShowInforEvent);
			this.addEventListener(MouseEvent.MOUSE_OUT,HideInforEvent);	
			PokemonInforPanel.visible=false;		
			LoadPokedexIMG.visible=false;		
		}
		public function addEventItemInBattle(its:Itemob){
			itemOut=its;
			this.addEventListener(MouseEvent.CLICK,useItemInBattle);	
		}
		function useItemInBattle(e:MouseEvent){			
			trace("Click "+itemOut.mItem.Name);
			if (itemOut.mItem.NumberOf>0 && !Gamedt.BattleUI.MyPokemon.UsingItem) {
				if(pokemon.AttackMC!=null){
					pokemon.AttackMC.UseItem(itemOut.mItem);
				} else if(itemOut.mItem.Type == "Potion"){
					pokemon.UseItem(itemOut.mItem);
					Gamedt.BattleUI.MyPokemon.DelayUseItem(3);
				}
				itemOut.reText();
				ReloadText();
			} else {
				addChild(new FloatingText("KHONG THE SU DUNG",200,0));
			}
		}
		//--------------
		public function addEventItemOutBattle(its:Itemob){
			itemOut=its;
			this.addEventListener(MouseEvent.CLICK,useItemOutBattle);	
		}
		function useItemOutBattle(e:MouseEvent){			
			trace("Click "+itemOut.mItem.Name);
			if (itemOut.mItem.NumberOf>0) {
				pokemon.UseItem(itemOut.mItem);
				itemOut.reText();
				ReloadText();
			} else {
				addChild(new FloatingText("KHONG CON ITEM",200,0));
			}
		}
		private function ShowInforEvent(e:MouseEvent) {
			PokemonInforPanel.visible=true;
			LoadPokedexIMG.visible=true;		
		}
		private function HideInforEvent(e:MouseEvent) {
			PokemonInforPanel.visible=false;
			LoadPokedexIMG.visible=false;		
		}
		private function LoadMovesPanel() {
			for (var i:int=1; i<5; i++) {
				PokemonInforPanel["Move"+i].TNumber.text=pokemon["Move"+i].PP+"MP";
				PokemonInforPanel["Move"+i].TName.text=pokemon["Move"+i].Name;
			}
			PokemonInforPanel.Ability.text=pokemon.Ability;
			PokemonInforPanel.Stats.text=pokemon.Attack+"-"+pokemon.Defense+"-"+pokemon.SpAtk+"-"+pokemon.SpDef+"-"+pokemon.Speed;			
			PokemonInforPanel.visible=false;
		}
		//----------------------------------------
		public function AddEventReplacePokemon(_replacePokemon:Pokemondt){
			replacePokemon=_replacePokemon;
			this.addEventListener(MouseEvent.CLICK, ClickReplacePokemonEvent);			
		}			
		private function ClickReplacePokemonEvent(evt:MouseEvent) {
			var targetPos:Number=evt.target.pokemon.GetMyPositionInList();				
			if(targetPos>=0){
				replacePokemon.upid=Gamedt.Player.pokemonList[targetPos].upid
				Gamedt.Player.pokemonList[targetPos]=replacePokemon;
				Gamedt.updatePokemon.Update(Gamedt.Player.pokemonList[targetPos]);
				this.parent.addChild(new FloatingText("THẢ!",310,155,2));
			}
		}
		//----------------------------------------
		public function AddEventSwitchPokemon(){
			this.addEventListener(MouseEvent.CLICK, ClickSwitchPokemonEvent);			
		}
		private function ClickSwitchPokemonEvent(evt:MouseEvent) {
			if (pokemon.CurrentHP>0) {				
				var myPos:Number=pokemon.GetMyPositionInList();
				var temPokemon:Pokemondt;
				trace("myPos "+myPos);
				temPokemon=Gamedt.Player.pokemonList[myPos];
				Gamedt.Player.pokemonList[myPos]=Gamedt.Player.pokemonList[0];				
				Gamedt.Player.pokemonList[0]=temPokemon;
				Gamedt.MainUI.GameMenu.PokemonPanel.render();
				Gamedt.updatePlayer.Update("leadpkm",Gamedt.Player.pokemonList[0].upid);
				//Gamedt.MainUI.loadPokemonList();
				//var Query:String="UPDATE twalk_user SET leadpkm='"+Gamedt.Player.pokemonList[0].upid+"'  WHERE uid='"+Gamedt.uid+"'";
				//Gamedt.SendQuery(Query);
			}
		}
		public function ReloadText() {
			if (pokemon.Status!="None" && pokemon.Status!="") {
				txtName.text=pokemon.Name+" "+pokemon.Status;
			} else {
				txtName.text=pokemon.Name;
			}
			txtHP.text=String("HP:"+pokemon.CurrentHP);
		}
	}
	
}
